package xin.clips.web.controller.gy;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.enums.gy.InnerMessageStatusEnum;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.GyInnerMessage;
import xin.clips.system.domain.gy.GyUserShareDoc;
import xin.clips.system.service.gy.IGyInnerMessageService;

/**
 * 站内信Controller
 *
 * @author clips
 * @date 2023-03-10
 */
@Controller
@RequestMapping("/gy/innerMessage")
public class GyInnerMessageController extends BaseController
{
    private String prefix = "gy/innerMessage";

    @Autowired
    private IGyInnerMessageService gyInnerMessageService;

    @VisistLog(title = "站内信")
    @GetMapping()
    public String innerMessage()
    {
        return prefix + "/innerMessage";
    }

    /**
     * 查询站内信列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyInnerMessage gyInnerMessage)
    {
        startPage();
        if(!ShiroUtils.getLoginName().equals("admin")){
            gyInnerMessage.setAcceptUserId(ShiroUtils.getUserId());
        }
        List<GyInnerMessage> list = gyInnerMessageService.selectGyInnerMessageList(gyInnerMessage);
        return getDataTable(list);
    }

    /**
     * 导出站内信列表
     */

    @Log(title = "站内信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyInnerMessage gyInnerMessage)
    {
        List<GyInnerMessage> list = gyInnerMessageService.selectGyInnerMessageList(gyInnerMessage);
        ExcelUtil<GyInnerMessage> util = new ExcelUtil<GyInnerMessage>(GyInnerMessage.class);
        return util.exportExcel(list, "站内信数据");
    }

    /**
     * 新增站内信
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存站内信
     */

    @Log(title = "站内信", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyInnerMessage gyInnerMessage)
    {
        return toAjax(gyInnerMessageService.insertGyInnerMessage(gyInnerMessage));
    }

    /**
     * 修改站内信
     */
    @GetMapping("/edit/{messageId}")
    public String edit(@PathVariable("messageId") Long messageId, ModelMap mmap)
    {
        GyInnerMessage gyInnerMessage = gyInnerMessageService.selectGyInnerMessageByMessageId(messageId);
        mmap.put("gyInnerMessage", gyInnerMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存站内信
     */

    @Log(title = "站内信", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyInnerMessage gyInnerMessage)
    {
        return toAjax(gyInnerMessageService.updateGyInnerMessage(gyInnerMessage));
    }

    /**
     * 删除站内信
     */
    @Log(title = "站内信", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gyInnerMessageService.deleteGyInnerMessageByMessageIds(ids));
    }

    /**
     *  标记为已读
     */
    @PostMapping("/setRead")
    @ResponseBody
    public AjaxResult setRead(GyInnerMessage innerMessage) {
        GyInnerMessage dbMsg = gyInnerMessageService.selectGyInnerMessageByMessageId(innerMessage.getMessageId());
        dbMsg.setInnerMessageStatus(Integer.parseInt(InnerMessageStatusEnum.READED.getCode()));
        return toAjax(gyInnerMessageService.updateGyInnerMessage(dbMsg));
    }
}
